package fr.dauphine.spring.bo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.SQLDelete;
import org.springframework.stereotype.Component;

@Component
@Entity
@Table(name = "CLIENT")
@SQLDelete(sql = "update CLIENT set deleted = 'TRUE' where id = ? and version = ?")
/* @Proxy(lazy=false) //pas de proxy */
public class Client extends BO implements Serializable, Comparable<Client> {

	private static final long serialVersionUID = 1L;

	@Column(length = 100)
	private String nom = "";

	@Column(length = 50)
	private String prenom = "";

	private Date dateNaissance;

	@Lob
	@Column(columnDefinition = "bytea")
	// @Column(length = 1000000)
	private byte[] photo;

	@Column(length = 100)
	private String photoType;

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
	private List<Adresse> adresses;

	public List<Adresse> getAdresses() {
		return adresses;
	}

	public void setAdresses(ArrayList<Adresse> adresses) {
		this.adresses = adresses;
	}

	/********* Listes des produits relatifs à un client donné **********/

	@OneToMany(cascade = CascadeType.REMOVE)
	private List<Commande> lstCommandes;

	public List<Commande> getLstCommandes() {
		return lstCommandes;
	}

	public void setLstCommandes(List<Commande> lstCommandes) {
		this.lstCommandes = lstCommandes;
	}

	/*******************************************************************/

	public Client() {
		adresses = new ArrayList<Adresse>();
		lstCommandes = new ArrayList<Commande>();
	}

	public Client(String id) {
		super(id);
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public byte[] getPhoto() {
		return photo;
	}

	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}

	public String getPhotoType() {
		return photoType;
	}

	public void setPhotoType(String photoType) {
		this.photoType = photoType;
	}

	public Date getDateNaissance() {
		return dateNaissance;
	}

	public void setDateNaissance(Date dateNaissance) {
		this.dateNaissance = dateNaissance;
	}

	public int compareTo(Client client) {
		return 0;
	}

	public boolean isNew() {
		return this.id == null || this.id.trim().equals("");
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Client) {
			final Client client = (Client) obj;
			return (client.nom.equals(this.nom) && client.prenom.equals(this.prenom));
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (id != null) ? id.hashCode() : 0;
	}

	@Override
	public String toString() {
		return nom + " " + prenom + " " + dateNaissance;
	}
}
